package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook,HttpSession session)
    {
        addressBook.setUserId((Long) session.getAttribute("user"));
        log.info("addressBook: "+addressBook);
        addressBookService.save(addressBook);
        String key="address_list_"+session.getAttribute("user");
        redisTemplate.delete(key);
        return R.success(addressBook);
    }
    @PutMapping("/default")
    public R<AddressBook>setDefault(@RequestBody AddressBook addressBook, HttpSession session)
    {
        LambdaUpdateWrapper<AddressBook>wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,session.getAttribute("user"));
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        addressBook.setIsDefault((1));
        addressBookService.updateById(addressBook);
        String key="address_list_"+session.getAttribute("user");
        redisTemplate.delete(key);
        return R.success(addressBook);
    }
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id)
    {
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook!=null)
        {
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }
    @GetMapping("default")
    public R<AddressBook>getDefault(HttpSession session)
    {
        LambdaQueryWrapper<AddressBook>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,session.getAttribute("user"));
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if(null==addressBook)
        {
            return R.error("没有找到该对象");
        }
        return R.success(addressBook);
    }
    @GetMapping("/list")
    public R<List<AddressBook>>list(AddressBook addressBook,HttpSession session)
    {
        String key="address_list_"+session.getAttribute("user");
        List<AddressBook>list = (List<AddressBook>) redisTemplate.opsForValue().get(key);
        if(list!=null){
            return R.success(list);
        }
        addressBook.setUserId((Long) session.getAttribute("user"));
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(null!=addressBook.getUserId(),AddressBook::getUserId,addressBook.getUserId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list1 = addressBookService.list(lambdaQueryWrapper);
        redisTemplate.opsForValue().set(key,list1,60, TimeUnit.MINUTES);
        return R.success(list1);
    }
    @PutMapping()
    public R<String>update(@RequestBody AddressBook addressBook)
    {
        addressBookService.updateById(addressBook);
        String key="address_list_"+addressBook.getUserId();
        redisTemplate.delete(key);
        return R.success("更新成功");
    }
    @DeleteMapping()
    public R<String>delete(@RequestParam List<Long>ids,HttpSession session)
    {
       addressBookService.removeByIds(ids);
        String key="address_list_"+session.getAttribute("user");
        redisTemplate.delete(key);
        return R.success("删除成功");
    }
}
